using UnityEngine;
using System.Collections;

public class CatManager : MonoBehaviour 
{
    public float timerOfRespawn;
    public float timer;
    public int spawnMaxNumber;
    public Cat[] cats;
    private static Dificult dificult;
    private int catsLimit;
    private static bool running;

	void Start () 
    {
		ChangeDificult(Dificult.Level1);
        running = true;
	}
	
	void Update () 
    {
        if(running)
        {
            timer += Time.deltaTime;

            if(timer >= timerOfRespawn)
            {
                StartCoroutine("InstantiateCats");
                timer = 0;
            }
        }
	}

    private IEnumerator InstantiateCats()
    {
        for(int i = 0; i < Random.Range(1, spawnMaxNumber); i++)
        {
            Instantiate(cats[Random.Range(0, catsLimit)], transform.position, Quaternion.identity);
            yield return new WaitForSeconds(0.5f);
        }
    }
	
	public static Dificult CheckDificult()
	{
		return dificult;
	}

    public void ChangeDificult(Dificult dificult)
    {
		switch (dificult) 
		{
		case Dificult.Level1: 
            timerOfRespawn = 5;
            timer = 0;
            spawnMaxNumber = 1;
            catsLimit = cats.Length/5;
			break;
        case Dificult.Level2: 
            timerOfRespawn = 4;
            timer = 0;
            spawnMaxNumber = 2;
            catsLimit = cats.Length/4;
     		break;
		case Dificult.Level3:
            timerOfRespawn = 3;
            timer = 0;
            spawnMaxNumber = 3;
            catsLimit = cats.Length/3;
			break;
		case Dificult.Level4:
            timerOfRespawn = 2;
            timer = 0;
            spawnMaxNumber = 4;
            catsLimit = cats.Length/2;
			break;
		case Dificult.Level5:
            timerOfRespawn = 1;
            timer = 0;
            spawnMaxNumber = 5;
            catsLimit = cats.Length;
    		break;	
		default:
			break;
		}
        
      	CatManager.dificult = dificult;
    }

    public static void Stop()
    {
        running = false;
    }
}